package com.applet.mallappletbackstage.controller;

import com.applet.mallappletbackstage.utils.R;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author X
 * @version 1.0
 * @ClassName DouYinApiController
 * @description 抖音api
 * @date 2020/8/11
 **/
@RestController
@RequestMapping("/oauth")
public class DouYinApiController {

    @GetMapping("/wx/getOpenid/{code}")
    public R getWXOpenid(@PathVariable(value = "code")String code){
        String result = "";
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            //创建HttpClient对象
            httpClient = HttpClients.createDefault();
            //创建HttpGet对象
            HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/sns/jscode2session?js_code="+code+"&appid=wx0c82a1cc97ed8477&secret=c0536721ef1dbe2ba67a200b27bea727&grant_type=authorization_code");
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000)//设置连接超时时间
                    .setConnectionRequestTimeout(30000)//从连接池获取连接超时时间
                    .setSocketTimeout(30000)//设置读取数据超时时间
                    .build();
            httpGet.setConfig(requestConfig);
            response = httpClient.execute(httpGet);
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if(response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("返回结果："+result);
        return R.success(result);
    }

    @GetMapping("/dy/getOpenid/{code}")
    public R getDYOpenid(@PathVariable(value = "code")String code){
        String result = "";
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            //创建HttpClient对象
            httpClient = HttpClients.createDefault();
            //创建HttpGet对象
             HttpGet httpGet = new HttpGet("https://developer.toutiao.com/api/apps/jscode2session?code="+code+"&appid=tt4cfd3a40d3c73c4e&secret=88a5a8a77727c9235be0b686048256bd16af657e");
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000)//设置连接超时时间
                    .setConnectionRequestTimeout(30000)//从连接池获取连接超时时间
                    .setSocketTimeout(30000)//设置读取数据超时时间
                    .build();
            httpGet.setConfig(requestConfig);
            response = httpClient.execute(httpGet);
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if(response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("返回结果："+result);
        return R.success(result);
    }
}
